<?php

namespace Common\Lib\TagLib;

use Think\Template\TagLib;

class get extends TagLib {

    protected $tags = array(
        'article' => array('attr' => 'as,id,catid,num,tag,order', 'level' => 1),
        'category' => array('attr' => 'as,pid,type,num,order', 'level' => 1),
        'course' => array('attr' => 'as,id,num,order,recommend,hot', 'level' => 1),
        'slider' => array('attr' => 'as,id,code', 'level' => 1),
    );

    public function _article($attr, $content) {
        $where = [];
        if (!$attr['as']) {
            $attr['as'] = 'val';
        }
        if ($attr['id']) {
            $where['id'] = ['in', explode(',', $attr['id'])];
        }
        if ($attr['catid']) {
            $where['catid'] = ['in', explode(',', $attr['catid'])];
        }
        if ($attr['tag']) {
            $where['tag'] = ['&', pow(2, intval($attr['tag']) - 1)];
        }
        if (!$attr['num']) {
            $attr['num'] = 10;
        }
        if (!$attr['order']) {
            $attr['order'] = 'id DESC';
        }
        $parseStr = '<?php ';
        $parseStr .= '$__WHERE__ = ' . var_export($where, true) . ';';
        $parseStr .= '$__LIMIT__ = ' . '\'' . $attr['num'] . '\'' . ';';
        $parseStr .= '$__ORDER__ = ' . '\'' . $attr['order'] . '\'' . ';';
        $parseStr .= '$__LIST__ = D(\'Article\')->where($__WHERE__)->order($__ORDER__)->limit($__LIMIT__)->field(\'content\',true)->select()' . ';';
        $parseStr .= '$__LIST__ = D(\'Article\')->parseList($__LIST__)' . ';';
        $parseStr .= 'foreach($__LIST__ as $key=>$' . $attr['as'] . '): ';
        $parseStr .= ';?>';
        $parseStr .= $this->tpl->parse($content);
        $parseStr .= '<?php endforeach;?>';
        if (!empty($parseStr)) {
            return $parseStr;
        }
        return;
    }

    public function _category($attr, $content) {
        $where = [];
        if (!$attr['as']) {
            $attr['as'] = 'val';
        }
        if ($attr['type']) {
            $where['type'] = $attr['type'];
        }
        if ($attr['pid']) {
            $where['pid'] = $attr['pid'];
        }
        if (!$attr['num']) {
            $attr['num'] = 10;
        }
        if (!$attr['order']) {
            $attr['order'] = 'id DESC';
        }
        $parseStr = '<?php ';
        $parseStr .= '$__WHERE__ = ' . var_export($where, true) . ';';
        $parseStr .= '$__LIMIT__ = ' . '\'' . $attr['num'] . '\'' . ';';
        $parseStr .= '$__ORDER__ = ' . '\'' . $attr['order'] . '\'' . ';';
        $parseStr .= '$__LIST__ = M(\'ArticleCategory\')->where($__WHERE__)->order($__ORDER__)->select()' . ';';
        $parseStr .= '$__LIST__ = D(\'Article\')->parseCategory($__LIST__)' . ';';
        $parseStr .= 'foreach($__LIST__ as $key=>$' . $attr['as'] . '): ';
        $parseStr .= ';?>';
        $parseStr .= $this->tpl->parse($content);
        $parseStr .= '<?php endforeach;?>';
        if (!empty($parseStr)) {
            return $parseStr;
        }
        return;
    }

    public function _course($attr, $content) {
        $where = [
            'category' => 1,
            'closed' => 0
        ];
        if (!$attr['as']) {
            $attr['as'] = 'val';
        }
        if ($attr['id']) {
            $where['id'] = ['in', explode(',', $attr['id'])];
        }
        if ($attr['recommend']) {
            $where['recommend'] = 1;
        }
        if ($attr['hot']) {
            $where['hot'] = 1;
        }
        if (!$attr['num']) {
            $attr['num'] = 10;
        }
        if (!$attr['order']) {
            $attr['order'] = 'id DESC';
        }

        $parseStr = '<?php ';
        $parseStr .= '$__WHERE__ = ' . var_export($where, true) . ';';
        $parseStr .= '$__LIMIT__ = ' . '\'' . $attr['num'] . '\'' . ';';
        $parseStr .= '$__ORDER__ = ' . '\'' . $attr['order'] . '\'' . ';';
        $parseStr .= '$prefix = C(\'DB_PREFIX\')' . ';';
        $parseStr .= '$__LIST__ = D(\'Class\')->where($__WHERE__)->field($prefix.\'class.*,\'.$prefix.\'teacher.name as teacher_name\')->order($__ORDER__)->limit($__LIMIT__)->JOIN(\'LEFT JOIN __TEACHER__ ON __CLASS__.teacher_id = __TEACHER__.id\')->select()' . ';';
        $parseStr .= '$__LIST__ = D(\'Class\')->parseList($__LIST__)' . ';';
        $parseStr .= 'foreach($__LIST__ as $key=>$' . $attr['as'] . '): ';
        $parseStr .= ';?>';
        $parseStr .= $this->tpl->parse($content);
        $parseStr .= '<?php endforeach;?>';
        if (!empty($parseStr)) {
            return $parseStr;
        }
        return;
    }

    public function _slider($attr, $content) {
        if (!$attr['code'] && !$attr['id']) {
            return '';
        }
        $where = [
            'status' => 1,
        ];

        if (!$attr['as']) {
            $attr['as'] = 'val';
        }
        if ($attr['id']) {
            $where['id'] = $attr['id'];
        }
        if ($attr['code']) {
            $where['code'] = $attr['code'];
        }
        $slider = M('Slider')->where($where)->find();
        if (!$slider) {
            return '';
        }
        if (!$attr['num']) {
            $attr['num'] = 10;
        }
        $map = [
            'slider_id' => $slider['id'],
            '_string' => "(start_time = 0 OR start_time<= " . NOW_TIME . ") AND (end_time = 0 OR end_time >= ".NOW_TIME.")",
        ];
        $parseStr = '<?php ';
        $parseStr .= '$__WHERE__ = ' . var_export($map, true) . ';';
        $parseStr .= '$__ORDER__ = \'sort ASC,id ASC \';';
        $parseStr .= '$__LIMIT__ = ' . '\'' . $attr['num'] . '\'' . ';';
        $parseStr .= '$prefix = C(\'DB_PREFIX\')' . ';';
        $parseStr .= '$__LIST__ = D(\'SliderItem\')->where($__WHERE__)->order($__ORDER__)->limit($__LIMIT__)->select()' . ';';      
        $parseStr .= 'foreach($__LIST__ as $key=>$' . $attr['as'] . '): ';
        $parseStr .= ';?>';
        $parseStr .= $this->tpl->parse($content);
        $parseStr .= '<?php endforeach;?>';
    
        if (!empty($parseStr)) {
            return $parseStr;
        }
        return;
    }

}
